import { getLogger } from 'log4js';
import "reflect-metadata";
import * as Koa from "koa"
import { createConnection } from 'typeorm'
import * as KoaLogger from "koa-logger"
import { useContainer, useKoaServer } from "routing-controllers";
import { Container } from "typedi";
import { auth } from "./middlewares/AuthMiddewares"
import * as cors from "koa2-cors"
import * as staticFiles from 'koa-static'
import * as path from "path"
const logger = getLogger("assess")


/**
 * Setup routing-controllers to use typedi container.
 */
useContainer(Container);

const koaLogger = KoaLogger((str) => {
    logger.info(str)
})

const koaApp = new Koa()

koaApp.use(koaLogger)
koaApp.use(staticFiles(path.join(__dirname, '../public')))
koaApp.use(cors())
koaApp.use(auth)
/**
 * We create a new koa server instance.
 * We could have also use useKoaServer here to attach controllers to an existing koa instance.
 */
useKoaServer(koaApp, {
    /**
     * We can add options about how routing-controllers should configure itself.
     * Here we specify what controllers should be registered in our express server.
     */
    development: true,
    routePrefix: "/api",
    controllers: [__dirname + "/controllers/*.js"],
    middlewares: [__dirname + "/middlewares/*.js"],
    defaultErrorHandler: true
});
koaApp.on("error", function (err) {
    console.log(err)
})
/**
 * Start the koa app.
 */

createConnection().then(connection => {
    koaApp.listen(3000);
    console.log("Server is up and running at port 3000");
}).catch(err => {
    console.log("TypeORM connection error: ", err)
})

